<template>
    <div class="default-main ba-table-box loading">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info"
                  show-icon/>

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'unfold', 'columnDisplay']"
            quick-search-placeholder="通过标题模糊搜索"
        />
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" :pagination="false"/>

        <!-- 表单 -->
        <PopupForm/>
    </div>
</template>

<script setup lang="ts">
import {ref, onMounted, provide} from 'vue'
import PopupForm from './popupForm.vue'
import Table from '/src/components/table/index.vue'
import TableHeader from '/src/components/table/header/index.vue'
import {defaultOptButtons} from '/src/components/table'
import {baTableApi} from '/src/api/common'
import baTableClass from '/src/utils/baTable'
import {concat} from "lodash-es";

let optButtons: OptButton[] = [
    {
        render: 'tipButton',
        name: 'info',
        title: '增加子级',
        text: '增加子级',
        type: 'success',
        icon: 'el-icon-SuccessFilled',
        class: 'table-row-edit',
        disabledTip: false,
        click: (row: TableRow) => {
            baTable.form.defaultItems.pid = row.id
            //调用表格头ADD按钮，如果data(number)传值>0 赋值 反之赋值adminInfo里的tissue_id
            baTable.onTableHeaderAction("add", {})

        },
    },
]

let Buttons = concat(optButtons, defaultOptButtons())
const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi('/admin_api/tissue/'),
    {
        expandAll: false,
        dblClickNotEditColumn: ['all'],
        column: [
            {type: 'selection', align: 'center'},
            {label: '名称', prop: 'name', align: 'left', 'show-overflow-tooltip': true},

            {label: '状态', prop: 'status', align: 'center', width: '80', render: 'switch'},
            {label: '创建时间', prop: 'create_time', align: 'center', width: '160', render: 'datetime'},
            {label: '修改时间', prop: 'update_time', align: 'center', width: '160', render: 'datetime'},

            {
                label: '操作',
                align: 'center',
                width: '300',
                render: 'buttons',
                buttons: Buttons,
            },
        ],
        dragSortLimitField: 'pid',
    },
    {
        defaultItems: {
            status: '1',
            pid: 0,
        },
    },
    {
        getIndex: () => {
            baTable.table.expandAll = baTable.table.filter?.quick_search ? true : false
        },
        // 获得编辑数据后
        requestEdit: () => {
            if (baTable.form.items && !baTable.form.items.icon) baTable.form.items.icon = 'el-icon-Minus'
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.dragSort()
    })
})
</script>

<script lang="ts">
import {defineComponent} from 'vue'

export default defineComponent({
    name: 'auth/menu',
})
</script>

<style scoped lang="scss"></style>
